package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/5/13 16:48
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    // http://localhost:8080 /customer/list

    /**
     * 客户列表分页
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("/list")
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        return customerService.list(page,rows,customerName);
    }

    // http://localhost:8080/ customer/save?customerId=1

    /**
     * 添加或修改客户
     * @param customer
     * @param customerId
     * @return
     */
    @PostMapping("/save")
    public ServiceVO save(Customer customer,Integer customerId){
        try {
            if (!StringUtils.isEmpty(customerId)){
                customerService.updateCustomer(customer);
            }else {
                customerService.save(customer);
            }
        } catch (Exception e) {
            return new ServiceVO(400, "保存失败！");
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    // http://localhost:8080/customer/delete
    /**
     * 批量删除客户
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ServiceVO deleteCustomer(String ids){
        try {
            customerService.deleteCustomer(ids);
        } catch (Exception e) {
            return new ServiceVO(400, "删除失败！");
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
